package com.xiaoc.bulldozer.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.xc.framework.common.job.OneMinuteJobService;
import com.xiaoc.bulldozer.common.dao.UserRoleMapper;
import com.xiaoc.bulldozer.common.dto.UserRolesQueryDto;
import com.xiaoc.bulldozer.common.model.UserRole;
import com.xiaoc.bulldozer.common.service.UserRoleService;
import com.xiaoc.bulldozer.common.service.cache.UserRoleCacheService;

/**
 * UserRole Service 实现类
 * 
 * @author CodeGenerator
 * 
 * @date 2014-06-03 18:02:39
 * 
 */
@Service("userRoleService")
public class UserRoleServiceImpl implements UserRoleService, UserRoleCacheService, OneMinuteJobService {

    /**
     * 用户角色Id列表Map，一分钟加载一次到内存
     */
    private Map<Integer, List<Integer>> userRoleListCacheMap = new HashMap<Integer, List<Integer>>();
    
    @Resource
    private UserRoleMapper userRoleMapper;

    @Override
    public void refreshCacheData() {
    
        Map<Integer, List<Integer>> userRoleListMap = new HashMap<Integer, List<Integer>>();
        List<UserRolesQueryDto> userRoleDtoList = getAllUserRoleList();
        
        for (UserRolesQueryDto userRoleDto : userRoleDtoList) {
            String roleIds = userRoleDto.getRoleIds();
            if (StringUtils.isNotEmpty(roleIds)) {
                List<Integer> roleIdList = new ArrayList<Integer>();
                String[] roleIdArry = roleIds.split(",");
                for (String roleId : roleIdArry) {
                    roleIdList.add(Integer.valueOf(roleId));
                }
                
                userRoleListMap.put(userRoleDto.getUserId(), roleIdList);
            }
        }
        
        userRoleListCacheMap = userRoleListMap;
    }
    
    @Override
    public List<Integer> getRoleIdListByUserId(Integer userId) {
        
        if (MapUtils.isEmpty(userRoleListCacheMap)) {
            refreshCacheData();
        }
        
        return userRoleListCacheMap.get(userId);
    }
    
    @Override
    public void saveBatch(List<UserRole> userRoleList) {
        
        if (CollectionUtils.isEmpty(userRoleList)) {
            return;
        }
        
        userRoleMapper.saveBatch(userRoleList);
    }

    @Override
    public void deleteByUserId(Integer userId) {
        userRoleMapper.deleteByUserId(userId);
    }

    @Override
    public List<UserRole> getUserRoleListByUserId(Integer userId) {
        return userRoleMapper.getUserRoleListByUserId(userId);
    }

    @Override
    public void grantRolesToUser(Integer userId, List<Integer> roleIdList) {

        if (CollectionUtils.isEmpty(roleIdList)) {
            return;
        }
        
        // 删掉已存在的userRole
        userRoleMapper.deleteByUserId(userId);        

        List<UserRole> userRoleList = new ArrayList<UserRole>();

        for (Integer roleId : roleIdList) {
            UserRole userRole = new UserRole();
            userRole.setRoleId(roleId);
            userRole.setUserId(userId);
            userRoleList.add(userRole);
        }

        this.saveBatch(userRoleList);
        
        // 权限变更后立刻刷新缓存生效
        refreshCacheData();
    }

    @Override
    public List<UserRolesQueryDto> getAllUserRoleList() {
        return userRoleMapper.getAllUserRoleList();
    }
}
